home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / eflibpt4.zip / DEMO / DATATYPE / PQUEUE.PAS < prev    next >
Pascal/Delphi Source File  |  1996-08-18  |  2KB  |  53 lines

  1. { Borland Pascal Extended Function Library - EFLIB (C) Johan Larsson, 1996
  2.   Demonstration; priority queue implemented as a heap
  3.  
  4.   EFLIB IS PROTECTED BY THE COPYRIGHT LAW AND MAY NOT BE COPIED, SOLD OR
  5.   MANIPULATED. FOR MORE INFORMATION, SEE PROGRAM MANUAL! THIS DEMONSTRAT-
  6.   ION PROGRAM MAY FREELY BE USED AND DISTRIBUTED.                          }
  7.  
  8.  
  9. uses EFLIBDEF, EFLIBINI, EFLIBDAT;
  10.  
  11. const NumberOfElements = 20;
  12.  
  13. var PriorityQueue : PriorityQueueObjectType;
  14.     Number        : byte;
  15.     Index         : byte;
  16.  
  17.  
  18. begin
  19.      Randomize;
  20.  
  21.      WriteLn ('* Priority queue based on a heap with dynamic allocation *');
  22.  
  23.      with PriorityQueue do begin
  24.           Initialize (NumberOfElements, SizeOf(Number));
  25.  
  26.           SetKey (1, SizeOf(Number));
  27.  
  28.           for Index := 1 to NumberOfElements do begin
  29.               Number := Succ(Random(High(Number)));
  30.               Store (Number);
  31.           end;
  32.  
  33.           { When elements are retrieved from a priority queue, the biggest
  34.             element is always the first to be retrieved. By creating your
  35.             own priority queue object and override the Compare method, you
  36.             can change the order of the priority queue. You must override
  37.             this method if your element data don't support byte-wise
  38.             comparison (variables such as words or integers doesn't). If you
  39.             want just a region of the element to be compared, set the key
  40.             region with the SetKey method. }
  41.  
  42.           while not IsEmpty do begin
  43.                 Retrieve (Number);
  44.                 Write (Number, ' ');
  45.           end;
  46.  
  47.           WriteLn;
  48.  
  49.           Intercept;
  50.      end;
  51.  
  52.      if GlobalDataError then WriteLn ('Error(s) reported!');
  53. end.